package com.loong.musicdemo.util.dataDeal;

import android.util.Pair;

import com.loong.musicdemo.model.SongList;
import com.loong.musicdemo.util.HostUtil;
import com.loong.musicdemo.util.httpUtil.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 歌单信息处理类
 */
public class PlayListDeal {
    private static final String HOST = HostUtil.getHost();

    /**
     * 获取热门歌单分类
     */
    public static List<String> getPlayHotList() {
        String url = HOST + "/playlist/hot";
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<String> playHot = new ArrayList<>();
        try {
            if (response != null) {
                JSONObject object = new JSONObject(response);
                JSONArray data = object.optJSONArray("tags");
                if (data != null && data.length() > 0) {
                    for (int i = 0; i < data.length(); i++) {
                        String name = data.getJSONObject(i).getString("name");
                        playHot.add(name);
                    }
                    return playHot;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return playHot;
    }

    /**
     * 通过tag标志获取歌单
     *
     * @param tag 标志,如华语
     * @return 歌单列表&时间戳
     */
    public static List<SongList> getPlayListByTag(String tag, int page) {
        List<SongList> songLists = new ArrayList<>();
        //精品加这个/highquality
        int offset = page * 12;
        String url = HOST + "/top/playlist?offset=" + offset + "&limit=12&cat=" + tag;//一次性获取12条数据
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        try {
            if (response != null) {
                JSONObject object = new JSONObject(response);
                JSONArray jsonArray = object.optJSONArray("playlists");
                if (jsonArray != null && jsonArray.length() > 0) {

                    for (int i = 0; i < jsonArray.length(); i++) {
                        SongList songList = new SongList();
                        //格式化歌单信息
                        JSONObject list = jsonArray.getJSONObject(i);
                        songList.setBackgroundUrl(list.getString("coverImgUrl"));
                        songList.setSongListId(list.getString("id"));
                        songList.setName(list.getString("name"));
                        songList.setPlayCount(list.getString("playCount"));
                        songList.setSongIds(MusicDeal.getSongOfSongListsId(songList.getSongListId()));
                        String nickName = list.getJSONObject("creator").getString("nickname");
                        songList.setCreatorName(nickName);
                        songLists.add(songList);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return songLists;
    }

    /**
     * 获取用户所有的歌单信息
     * 第一个歌单是我喜欢的音乐
     *
     * @return 用户所有歌单列表信息
     */
    public static List<SongList> getUserAllPlayListInfo(String uid) {
        String url = HOST + "/user/playlist?uid=" + uid;
        List<SongList> songLists = new ArrayList<>();
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        try {
            JSONObject object = new JSONObject(response);
            JSONArray playlist = object.getJSONArray("playlist");
            for (int i = 0; i < playlist.length(); i++) {
                JSONObject listObject = playlist.getJSONObject(i);
                JSONObject creator = listObject.getJSONObject("creator");
                SongList s = new SongList();
                s.setSongListId(listObject.getString("id"));
                s.setPlayCount(listObject.getString("playCount"));
                s.setCommentCount(MusicDeal.getPlaylistCommentCount(s.getSongListId()));
                s.setBackgroundUrl(listObject.getString("coverImgUrl"));
                s.setName(listObject.getString("name"));
                s.setCreatorName(creator.getString("nickname"));
                s.setCreatorId(creator.getString("userId"));
                s.setCreatorAvatarUrl(creator.getString("avatarUrl"));
                s.setSongIds(MusicDeal.getSongOfSongListsId(s.getSongListId()));
                songLists.add(s);
            }
            return songLists;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return songLists;
    }
}
